package com.kaibes.module.file.local.db;

import java.math.BigInteger;
import java.util.List;

import org.springframework.stereotype.Service;

import com.kaibes.orm.core.core.Page;
import com.kaibes.orm.curd.QueryBuilder;
import com.kaibes.web.api.ResponseServiceImpl;

@Service
public class FileMarkService extends ResponseServiceImpl<FileMark> {

    public boolean hasFileByFileInfoId(BigInteger fileInfoId) {
        return count(QueryBuilder.newBuilder().eq("file_info_id", fileInfoId).build())>0;
    }
    
    public List<FileMark> listByFileInfoId(BigInteger fileInfoId) {
        return list(QueryBuilder.newBuilder().eq("file_info_id", fileInfoId).build(), "uuid");
    }
    public boolean removeByFileInfoId(BigInteger fileInfoId) {
        return remove(QueryBuilder.newBuilder().eq("file_info_id", fileInfoId).build())>0;
    }
    
    public boolean removeByUuid(String uuid) {
        return remove(QueryBuilder.newBuilder().eq("uuid", uuid).build())>0;
    }
    
    public boolean removeByUuidIn(List<String> uuidList) {
        return remove(QueryBuilder.newBuilder().inList("uuid", uuidList).build())>0;
    }
    
    public boolean removeByUserId(Integer userId) {
        return remove(QueryBuilder.newBuilder().eq("user_id", userId).build())>0;
    }
    
    public FileMark getByUuid(String uuid) {
        return get(QueryBuilder.newBuilder().eq("uuid", uuid).build());
    }
    
    public Page<FileMark> pageByUuidLikeOrNameLike(int pageIndex, int pageSize, String search) {
        return page(pageIndex, pageSize, query().rightLike("uuid", search).build(), query().like("name", search).build());
    }
    
    public long countByUserId(Integer userId) {
    	return super.count(query().eq("user_id", userId).build());
    }
    
    public Page<FileMark> pageByUserIdAndUuidLikeOrNameLike(int pageIndex, int pageSize, Integer userId, String search) {
        return page(pageIndex, pageSize, query().eq("user_id", userId).rightLike("uuid", search).build(), query().eq("user_id", userId).like("name", search).build());
    }
    
    public Page<FileMark> pageByUserId(int pageIndex, int pageSize, Integer userId) {
    	return super.page(pageIndex, pageSize, query().eq("user_id", userId).build());
    }
    
    public int removeByIdAndUserId(Object id, Integer userId) {
    	return super.remove(query().eq("id", id).eq("user_id", userId).build());
    }
    
    public int removeByIdsAndUserId(List<?> ids, Integer userId) {
    	return super.remove(query().inList("id", ids).eq("user_id", userId).build());
    }
    
}
